package com.grechur.wanandroidcompose.widget


import android.text.TextUtils
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.*
import androidx.compose.material.Divider
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import coil.request.ImageRequest
import com.grechur.wanandroidcompose.WanApplication
import com.grechur.wanandroidcompose.bean.article.Article
import com.grechur.wanandroidcompose.viewmodel.BaseViewModel
import com.grechur.wanandroidcompose.viewmodel.CollectViewModel
import com.grechur.wanandroidcompose.viewmodel.HomeViewModel
import com.grechur.wanandroidcompose.viewmodel.ProjectArticleViewModel
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@Composable
fun ArticlePage(
    globalNavHostController: NavHostController,
    article: Article,
    modifier: Modifier = Modifier,
    cid:String? = null,
    index:Int = -1,
    isArticles:Boolean = true,
    canCollect: Boolean = true) {
    val coroutineScope = rememberCoroutineScope()
    val viewModel = if (TextUtils.isEmpty(cid)) {
        viewModel(HomeViewModel::class.java)
    } else {
        viewModel(ProjectArticleViewModel::class.java,key = cid)
    }

    val collectViewModel = viewModel(CollectViewModel::class.java)

    Column(modifier = modifier) {
        Row(modifier = Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
            article.envelopePic.takeUnless { TextUtils.isEmpty(it) }
                ?.let { pic ->
                    AsyncImage(model = ImageRequest
                        .Builder(LocalContext.current)
                        .data(pic)
                        .crossfade(true)
                        .build(),
                        modifier = Modifier.size(50.dp, 50.dp),
                        contentScale = ContentScale.Crop,
                        contentDescription = null)
                }
            Column(modifier = Modifier.weight(1f)) {
                Text(text = article.title ?: "",
                    fontSize = 16.sp,
                    color = Color.Black,
                    modifier = Modifier.padding(top = 5.dp),
                    maxLines = 2, overflow = TextOverflow.Ellipsis
                )
                Row(modifier = Modifier.padding(top = 8.dp)) {
                    Text(text = article.chapterName ?: "",
                        fontSize = 14.sp,
                        color = Color.Gray)
                    article.author.takeIf { TextUtils.isEmpty(it) }
                        ?.let { author ->
                            Text(text = author,
                                fontSize = 14.sp,
                                color = Color.Gray,
                                modifier = Modifier.padding(start = 5.dp))
                        }
                    Text(text = article.niceShareDate ?: "",
                        fontSize = 14.sp,
                        color = Color.Gray,
                        modifier = Modifier.padding(start = 5.dp))
                }

            }
            AnimatedVisibility(visible = canCollect) {
                Row(verticalAlignment = Alignment.CenterVertically) {
                    IconButton(onClick = {
                        changeCollect(coroutineScope,globalNavHostController,index,article,isArticles,collectViewModel,viewModel)
                    }) {
                        Log.e("zhouzhu","Collect:${article.collect}")
                        val col = article.collect.takeIf { it != null && it }?.let { Color.Red }?:Color.Gray
                        Icon(imageVector = Icons.Default.Favorite, contentDescription = null, tint = col)
                    }
                    Text(text = "${article.zan}", fontSize =14.sp)
                }
            }

        }
        Divider(modifier = Modifier.padding(top = 5.dp))
    }
}

fun changeCollect(
    coroutineScope: CoroutineScope,
    globalNavHostController: NavHostController,
    index: Int,
    article: Article,
    isArticles: Boolean,
    collectViewModel: CollectViewModel,
    viewModel: BaseViewModel
) {
    coroutineScope.launch {
        if (!WanApplication.isLogin) {
            globalNavHostController.navigate("login")
            return@launch
        }
        if (article.collect == true) {//已经收藏了
            if (isArticles) {
                collectViewModel.unCollectArticle(article.id)
            } else {
                collectViewModel.unCollectList(article.id,article.originId)
            }
        } else {//还没收藏
            if (article.adminAdd == true) {//是否是站内文章
                collectViewModel.collectArticle(article.id)
            } else {
                collectViewModel.collectOtherArticle(article.title,article.author,article.link)
            }
        }
        collectViewModel.isCollect.let {
            Log.e("zhouzhu","isCollect:${it}")
            if (it) {
                viewModel.upItemData(index,true)
            } else {
                viewModel.upItemData(index,false)
            }
        }
    }
}


